package cn.oyjg.debugTool;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import cn.oyjg.debugTool.entity.Param;

public class ParamDialog extends JDialog {
	
	private Logger log=Logger.getLogger(ParamDialog.class);

	private final JPanel contentPanel = new JPanel();

	private App app=null;
	private JFrame frmv;
	private JTextArea textArea;
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	private JTextField txt_porpertyName;
	private JComboBox<String> cbx_porpertyName;
	private JCheckBox checkBox ;
	private List<String> properList;
	
	/**
	 * Create the dialog.
	 */
	public ParamDialog(App app,JFrame frmv,List<String> properList) {
		setTitle("\u751F\u6210\u53C2\u6570:\u7C98\u8D34\u5B9E\u4F53\u5230\u672C\u7A97\u53E3");
		this.frmv=frmv;
		this.app=app;
		this.properList=properList;
		setBounds(100, 100, 579, 447);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		
		
		
		textArea = new JTextArea();
		contentPanel.add(textArea);
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		JButton okButton = new JButton("\u786E\u5B9A");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				generaParam();
			}
		});
		checkBox = new JCheckBox("\u5BF9\u8C61\u5C5E\u6027");
		buttonPane.add(checkBox);
		checkBox.addItemListener(new ItemListener() {
 
        public void itemStateChanged(ItemEvent e) {
          Object obj = e.getItem();
            if(obj.equals(checkBox)) {
            	cbx_porpertyName.setVisible(checkBox.isSelected());
	        }
	      };
	    });
		
		cbx_porpertyName = new JComboBox<String>();
		for(int i=0;i<this.properList.size();i++){
			cbx_porpertyName.addItem(this.properList.get(i));
		}
		buttonPane.add(cbx_porpertyName);
//		txt_porpertyName.setColumns(10);
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		JButton cancelButton = new JButton("\u53D6\u6D88");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
	}
	private void generaParam(){
		try{
			List<Param> params=new ArrayList<Param>();
			if(this.textArea.getText().equals("")){
				return;
			}
			String[] rows=this.textArea.getText().split("\n");
			String pre="";
			if(this.checkBox.isSelected()){
				pre+=this.cbx_porpertyName.getSelectedItem()+".";
			}
			for(int i=0;i<rows.length;i++){
				if(rows[i].indexOf("private")>=0){
					
					//读取注释
					String comment=rows[i-2];
					if(comment!=null && !comment.equals("")){
						comment=comment.replaceAll("\\*", "");
					}
							
					String[] column=removeNull(rows[i].trim().split(";| "));
					String type=column[1];
					String name=column[2];
					if(type.equals("int")|| type.equals("Integer")|| type.equals("BigDecimal") || type.equals("long") || type.equals("Long")){
						params.add(new Param(pre+name,0));
					}else if(type.equals("Date")){
						params.add(new Param(pre+name,sdf.format(new Date())));
					}else if(type.equals("String")){
						params.add(new Param(pre+name,comment));
					}else{
						params.add(new Param(pre+name,comment));
					}
				}
			}
			if(this.checkBox.isSelected()){
				app.fillTable(params,"append");
			}else{
				app.fillTable(params,"clean");
			}
			setVisible(false);
		}catch(Exception err){
			log.error(err);
			app.lbl_status.setText(err.getMessage());
		}
	}
	
	private String[] removeNull(String[] column){
		List<String> tmp = new ArrayList<String>();
        for(String str:column){
            if(str!=null && str.length()!=0){
                tmp.add(str);
            }
        }
        return tmp.toArray(new String[0]);
	}
}
